package com.hunktimes.wholesale.ui.pages

import android.os.Bundle
import android.view.View
import android.widget.TabHost
import android.widget.TableLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.tabs.TabLayout
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BasePageFragment
import com.hunktimes.wholesale.databinding.FragmentCategorySearchBinding
import com.hunktimes.wholesale.util.L

/**
 * Author: Delete
 * Date: 2020/7/29 11:57
 * Description:
 *
 */
class CategorySearchFragment : BasePageFragment(R.layout.fragment_category_search) {

    override val titleRes: Int
        get() = R.string.title_category

    lateinit var viewBinding: FragmentCategorySearchBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentCategorySearchBinding.bind(view)

    }

    override fun onStart() {
        super.onStart()
        initTabLayout()
        initCategoryGoods()
    }

    val titles = arrayOf("分類1", "分類2", "分類3", "分類4", "分類5")

    fun initTabLayout() {
        titles.forEach {
            val tab = viewBinding.tabLayout.newTab()
            tab.setCustomView(R.layout.tab_view_simple)
            tab.customView!!.findViewById<TextView>(R.id.tab_text).text = it
            tab.view.setBackgroundColor(
                ContextCompat.getColor(
                    requireContext(),
                    android.R.color.transparent
                )
            )
            viewBinding.tabLayout.addTab(tab)
        }

        viewBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
                L.d("CategorySelectFragment OnTabSelectedListener onTabReselected invoke")
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                L.d("CategorySelectFragment OnTabSelectedListener onTabUnselected invoke")
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                L.d("CategorySelectFragment OnTabSelectedListener onTabSelected invoke")
            }

        })
        viewBinding.radioGroup.setOnCheckedChangeListener { group, checkedId ->
            L.d("CategorySelectFragment setOnCheckedChangeListener checkedId -> $checkedId")
        }
    }

    val categorySearchAdapter = CategorySearchAdapter()
    fun initCategoryGoods() {
        val categorySearcLm = LinearLayoutManager(requireContext())
        categorySearcLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.categorySearchRcv.layoutManager = categorySearcLm
        viewBinding.categorySearchRcv.adapter = categorySearchAdapter

        val data = mutableListOf<String>()
        for (i in 1..3) {
            data.add("CategorySearchAdapter $i")
        }
        categorySearchAdapter.data = data
    }
}

class CategorySearchAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_goods) {
    override fun convert(holder: BaseViewHolder, item: String) {

    }
}